﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using eCitizen.eAlerts.BusinessService;

namespace eCitizen.eAlerts.BatchFramework
{
    public abstract class ServiceBatchFramework
    {
        protected BatchErrorLogger _batchLogger;

        public void RunService(BatchBehavior currentBatch)
        {
            try
            {
                _batchLogger = new BatchErrorLogger(currentBatch);

                _batchLogger.LogInfo(String.Format("{0} started at : {1}", currentBatch.ToString(), DateTime.Now.ToString()));

                IServiceBase serviceToRun = GetService(currentBatch);
                serviceToRun.RunService();

                _batchLogger.LogInfo(String.Format("{0} ended at : {1}", currentBatch.ToString(), DateTime.Now.ToString()));
            }
            catch (Exception ex)
            {
                _batchLogger.LogException(ex);
            }
        }

        private IServiceBase GetService(BatchBehavior currentBatch)
        {
            IServiceBase serviceToRun = null;

            if (currentBatch == BatchBehavior.SendAlertService)
            {
                serviceToRun = new eCitizen.eAlerts.BusinessService.SendAlertService();
            }
            else if (currentBatch == BatchBehavior.SendReportService)
            {
                serviceToRun = new eCitizen.eAlerts.BusinessService.SendReportService();
            }
            else
            {
                serviceToRun = new eCitizen.eAlerts.BusinessService.HouseKeepingService();
            }

            return serviceToRun;
        }

    }
}
